package com.football;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;

public class CustomVideoCamera extends Activity {
			
			
	private static int VIDEO_CAPTURED = 1;
	private static final String MY_VIDEO_PATH = "myVideoPath";
	
	public String myVideoPath;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
        // Get values between activities
        Bundle extras = getIntent().getExtras();
        myVideoPath = extras.getString(MY_VIDEO_PATH);
        
		Intent captureVideoIntent = new Intent(
				android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
		startActivityForResult(captureVideoIntent, VIDEO_CAPTURED);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == VIDEO_CAPTURED) {
			try {
				Uri androidUri = data.getData();
				String path = getRealPathFromURI(androidUri);
				File file = new File(path);
				File myVideoFile = new File(myVideoPath);
				file.renameTo(myVideoFile);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		finish();
	}	
	
	// Retorna el path real del video gravat
	public String getRealPathFromURI(Uri contentUri) {
		
        String[] proj = { MediaStore.Images.Media.DATA };
        
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        
        return cursor.getString(column_index);
    }
}